package org.jsola.contract.web;

import org.jsola.contract.constant.ContractConstants;
import org.jsola.contract.dto.ContractVariableAddDTO;
import org.jsola.contract.dto.ContractVariableBatchDTO;
import org.jsola.contract.dto.ContractVariableUpdateDTO;
import org.jsola.contract.query.ContractVariableQuery;
import org.jsola.contract.service.IContractVariableService;
import org.jsola.contract.vo.ContractVariableListVO;
import org.jsola.contract.vo.ContractVariableVO;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 合同关联模板变量
 *
 * @author JanusMix
 */
@RestController("contractContractVariableController")
@RequestMapping(ContractConstants.API_PATH + "/v1/contractVariables")
public class ContractVariableController implements IContractVariableControllerAPI {

    @Autowired
    private IContractVariableService contractVariableService;

    @Override
    @PostMapping
    public Result<ContractVariableVO> save(@Valid @RequestBody ContractVariableAddDTO contractVariableAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增合同关联模板变量权限校验

        return Result.success(
                contractVariableService.save(contractVariableAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody ContractVariableUpdateDTO contractVariableUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改合同关联模板变量权限校验

        int count = contractVariableService.update(contractVariableUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @PostMapping(value = "/batch")
    public Result batch(@Valid @RequestBody ContractVariableBatchDTO contractVariableBatchDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量操作权限校验

        // 批量操作，注意校验，有越权风险
        // 新增
        List<ContractVariableAddDTO> contractVariableAddDTOList = contractVariableBatchDTO.getContractVariableAddDTOList();
        if (!CollectionUtils.isEmpty(contractVariableAddDTOList)) {
            contractVariableService.batchSave(contractVariableAddDTOList,tokenUser);
        }
        // 修改
        List<ContractVariableUpdateDTO> contractVariableUpdateDTOList = contractVariableBatchDTO.getContractVariableUpdateDTOList();
        if (!CollectionUtils.isEmpty(contractVariableUpdateDTOList)) {
            contractVariableUpdateDTOList.parallelStream().forEach(
                    contractVariableUpdateDTO -> {
                        contractVariableService.update(contractVariableUpdateDTO,tokenUser);
                    }
            );
        }
        // 删除
        List<Long> deleteContractVariableIdList = contractVariableBatchDTO.getDeleteContractVariableIdList();
        if (!CollectionUtils.isEmpty(deleteContractVariableIdList)) {
            contractVariableService.deleteByIds(tokenUser,deleteContractVariableIdList.toArray(new Long[0]));
        }
        return Result.success();
    }


    @Override
    @DeleteMapping(value = "/{contractVariableId}")
    public Result<Integer> delete(@PathVariable Long contractVariableId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除合同关联模板变量权限校验

        int count = contractVariableService.deleteByIds(tokenUser, contractVariableId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] contractVariableIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除合同关联模板变量权限校验

        int count = contractVariableService.deleteByIds(tokenUser, contractVariableIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{contractVariableId}")
    public Result<ContractVariableVO> findById(@PathVariable Long contractVariableId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看合同关联模板变量权限校验

        return Result.success(
            contractVariableService.selectById(contractVariableId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<ContractVariableListVO>> listAll(@Valid ContractVariableQuery contractVariableQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看合同关联模板变量权限校验

        return Result.success(
            contractVariableService.select(contractVariableQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<ContractVariableListVO>> page(@Valid ContractVariableQuery contractVariableQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            contractVariableService.selectPage(contractVariableQuery, tokenUser.getSiteId())
        );
    }


}

